<?php
/**
 * 后台主题管理
 *
 * 后台主题管理controller
 *
 * @author    zhouchao <zhouchao@yunys.com.cn>
 * @copyright 2016 QuanZhouYunYiShang Inc.
 */
namespace App\Http\Controllers\Backstage;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Services\TypesService\TypesBackService;
use App\Services\BrandService\BrandToolService;
use Illuminate\Support\Facades\Response;

class TypesController extends Controller
{
    public function __construct(TypesBackService $types_back_service,BrandToolService $brand_tool_service)
    {
        $this->types_back_service = $types_back_service;
        $this->brand_tool_service = $brand_tool_service;
    }

    /**
     * 获取主题列表
     * @return $this
     */
    public function getList()
    {
        $types = $this->types_back_service->getList(15);
        return view('backstage.types.list')
            ->with('types',$types);
    }

    /**
     * 更新主题页面
     * @param $type_id
     * @return $this
     */
    public function getUpdate($type_id)
    {
        $type = $this->types_back_service->get_type_by_type_id(intval($type_id));
        return view('backstage.types.update')
            ->with('type',$type);
    }

    /**
     * 更新主题
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function postUpdate(Request $request)
    {
        $this->validate($request,[
            'type_id' => 'required|integer',
            'type_name' => 'required|max:20|unique:types,type_name,'.$request->input('type_id').',type_id,state,1',
            'type_image'=>'image',
            'app_id' => 'required|integer',
            'is_show'=>'required|boolean',
            'is_features'=>'required|boolean',
        ]);
        $data = $request->all();
        unset($data['_token']);
        $result = $this->types_back_service->update_type($data);
        return empty($result) ? redirect()->back()->with('error_message','修改失败')
            : redirect()->back()->with('success_message','修改成功');
    }

    /**
     * 添加主题
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function getAdd()
    {
        $brands = $this->brand_tool_service->getAllBrands();
        return view('backstage.types.add')
            ->with('brands',$brands);
    }

    /**
     * 添加主题
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function postAdd(Request $request)
    {
        $this->validate($request,[
            'type_name' => 'required|max:20|unique:types,type_name,NULL,id,state,1',
            'type_image'=>'required|image',
            'app_id' => 'required|integer',
            'is_show'=>'required|boolean',
            'is_features'=>'required|boolean',
        ]);
        $data = $request->all();
        if(isset($data['product_ids'])){
            $product_ids = $data['product_ids'];
        }
        $result = $this->types_back_service->add_type($data);
        if(empty($result)){
            return redirect()->back()->with('error_message','添加失败');
        }
        //保存产品ID
        if(isset($product_ids) && !empty($product_ids)){
            $this->types_back_service->save_product_ids($product_ids,$result);
        }
        return empty($result) ?
            : redirect()->back()->with('success_message','添加成功');
    }

    /**
     * 删除主题
     * @param $type_id
     * @return \Illuminate\Http\RedirectResponse
     */
    public function getDelete($type_id)
    {
        $result = $this->types_back_service->update_type_state(intval($type_id));
        return empty($result) ? redirect()->back()->with('error_message','删除失败')
            : redirect()->back()->with('success_message','删除成功');
    }

    /**
     * 修改排序
     * @param Request $request
     * @return mixed
     */
    public function postUpdateSort(Request $request)
    {
        $type_id = intval($request->input('type_id'));
        $sort = intval($request->input('sort'));
        $result = $this->types_back_service->update_type_sort($type_id,$sort);
        return empty($result) ? Response::json(array('message'=>'修改失败','result'=>'','state'=>'M00001'))
            : Response::json(array('message'=>'修改成功','result'=>'','state'=>'M00000'));
    }

    /**
     * 修改主题显示状态
     * @param Request $request
     * @return mixed
     */
    public function postUpsateShow(Request $request)
    {
        $type_id = intval($request->input('type_id'));
        $is_show = intval($request->input('is_show'));
        $result = $this->types_back_service->change_type_show($type_id,$is_show);
        return empty($result) ? Response::json(array('message'=>'修改失败','result'=>'','state'=>'M00001'))
            : Response::json(array('message'=>'修改成功','result'=>'','state'=>'M00000'));
    }

    /**
     * 查找产品
     * @param Request $request
     * @return mixed
     */
    public function postSearch(Request $request)
    {
        $input = $request->all();
        unset($input['_token']);
        $products = $this->types_back_service->get_search_product_list($input);
        if(empty($products)){
            return response()->json(array('message'=>'未找到产品','result'=>'','state'=>'M00001'));
        }
        return response()->json(array('message'=>'成功','result'=>$products,'state'=>'M00000'));
    }

    /**
     * 获取该主题产品列表
     * @param $type_id
     * @return mixed
     */
    public function getProductList($type_id)
    {
        $products = $this->types_back_service->get_product_list_by_type_id(intval($type_id),15);
        return view('backstage.types.product_list')
            ->with('products',$products)
            ->with('type_id',$type_id);
    }

    /**
     * 修改主题产品排序
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function getUpdateProductSort(Request $request){
        $type_id = intval($request->input('type_id'));
        $sort = intval($request->input('sort')) <= 0 ? 1 : intval($request->input('sort'));
        $product_id = trim($request->input('product_id'));
        //$type_resource_id = intval($request->input('type_resource_id'));
        $result = $this->types_back_service->update_type_product_sort($type_id,$product_id,$sort);
        return empty($result) ? redirect()->back()->with('error_message','修改失败') :
            redirect()->back()->with('success_message','修改成功');
    }

    /**
     * 主题添加产品列表
     * @param $type_id
     * @return mixed
     */
    public function getAddProduct(Request $request,$type_id)
    {
        $input = $request->input('conditions');
        if(!empty($input)){
            $input = json_decode($input);
            if(empty($input)){
                return redirect()->back()->with('error_mesage','未找到符合条件的产品');
            }
            $products = $this->types_back_service->get_search_product_list((array)$input);
        }else{
            $products = $this->types_back_service->get_search_product_list(array('user_type' => '0','brand_id' => '0',
                'is_shelves' => '-1','user_param' => '0','product_id' => '0'));
        }
        $brands = $this->brand_tool_service->getAllBrands();
        return view('backstage.types.product_add')
            ->with('brands',$brands)
            ->with('type_id',$type_id)
            ->with('products',$products)
            ->with('conditions',$input);
    }

    /**
     * 主题添加产品
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function postAddProduct(Request $request)
    {
        $type_id = intval($request->input('type_id'));
        if(empty($type_id)){
            return redirect()->back()->with('error_message','请选择主题');
        }
        $product_ids = $request->input('product_ids');
        if(empty($product_ids)){
            return redirect()->back()->with('error_message','请选择产品');
        }
        $this->types_back_service->save_product_ids($product_ids,$type_id);
        return redirect()->back()->with('success_message','添加成功');
    }

    /**
     * 主题删除产品
     * @param $type_id
     * @param $product_id
     * @return \Illuminate\Http\RedirectResponse
     */
    public function getDeleteProduct($type_id,$product_id)
    {
        $result = $this->types_back_service->delete_type_product(intval($type_id),$product_id);
        return empty($result) ? redirect()->back()->with('error_message','删除失败') : redirect()->back()->with('success_message','删除成功');
    }
}
